package com.xwn.utils.core;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

/**
 * String 操作Util
 * @Author duandw
 * @Date 2019/12/31 16:48
 **/
public class SpringUtilsImpl implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public SpringUtilsImpl() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext = applicationContext;
    }

    public static <T> T getBean(String beanName, Class<T> clazz) {
        try {
            return applicationContext.getBean(beanName, clazz);
        } catch (Exception var3) {
            return null;
        }
    }

    public static <T> T getBean(String beanName) {
        try {
            return (T) applicationContext.getBean(beanName);
        } catch (Exception var2) {
            return null;
        }
    }

    public static <T> T getBean(Class<T> clazz) {
        try {
            return applicationContext.getBean(clazz);
        } catch (Exception var2) {
            return null;
        }
    }

    public static <T> Map<String, T> getBeansBy(Class<T> clazz) {
        return applicationContext.getBeansOfType(clazz);
    }
}
